package com.axzhengxin.platform.business.service.impl;

import java.util.Date;
import java.util.List;

import com.axzhengxin.platform.business.domain.BizFlowInfo;
import com.axzhengxin.platform.business.domain.BizFlowTask;
import com.axzhengxin.platform.business.domain.BizPledgeInfo;
import com.axzhengxin.platform.business.mapper.BizFlowInfoMapper;
import com.axzhengxin.platform.business.mapper.BizFlowTaskMapper;
import com.axzhengxin.platform.business.mapper.BizPledgeInfoMapper;
import com.axzhengxin.platform.business.service.IBizPledgeInfoService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 抵质押物Service业务层处理
 *
 * @author axzhengxin
 * @date 2021-03-16
 */
@Slf4j
@Service
public class BizPledgeInfoServiceImpl implements IBizPledgeInfoService {
    @Autowired
    private BizPledgeInfoMapper bizPledgeInfoMapper;

    @Autowired
    private BizFlowTaskMapper bizFlowTaskMapper;

    @Autowired
    private BizFlowInfoMapper bizFlowInfoMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 抵押创建
     */
    private String pledgeCreate = "PLEDGE_CREATE";
    /**
     * 抵押解除
     */
    private String pledgeDisable = "PLEDGE_DISABLE";

    /**
     * 查询抵质押物
     *
     * @param pledgeId 抵质押物ID
     * @return 抵质押物
     */
    @Override
    public BizPledgeInfo selectBizPledgeInfoById(String pledgeId) {
        return bizPledgeInfoMapper.selectBizPledgeInfoById(pledgeId);
    }

    /**
     * 查询抵质押物列表
     *
     * @param bizPledgeInfo 抵质押物
     * @return 抵质押物
     */
    @Override
    public List<BizPledgeInfo> selectBizPledgeInfoList(BizPledgeInfo bizPledgeInfo) {
        return bizPledgeInfoMapper.selectBizPledgeInfoList(bizPledgeInfo);
    }

    /**
     * 新增抵质押物
     *
     * @param bizPledgeInfo 抵质押物
     * @return 结果
     */
    @Override
    public int insertBizPledgeInfo(BizPledgeInfo bizPledgeInfo) {
        bizPledgeInfo.setCreateTime(DateUtils.getNowDate());
        return bizPledgeInfoMapper.insertBizPledgeInfo(bizPledgeInfo);
    }

    /**
     * 修改抵质押物
     *
     * @param bizPledgeInfo 抵质押物
     * @return 结果
     */
    @Override
    public int updateBizPledgeInfo(BizPledgeInfo bizPledgeInfo) {
        bizPledgeInfo.setUpdateTime(DateUtils.getNowDate());
        return bizPledgeInfoMapper.updateBizPledgeInfo(bizPledgeInfo);
    }

    /**
     * 批量删除抵质押物
     *
     * @param pledgeIds 需要删除的抵质押物ID
     * @return 结果
     */
    @Override
    public int deleteBizPledgeInfoByIds(String[] pledgeIds) {
        return bizPledgeInfoMapper.deleteBizPledgeInfoByIds(pledgeIds);
    }

    /**
     * 删除抵质押物信息
     *
     * @param pledgeId 抵质押物ID
     * @return 结果
     */
    @Override
    public int deleteBizPledgeInfoById(String pledgeId) {
        return bizPledgeInfoMapper.deleteBizPledgeInfoById(pledgeId);
    }

    /**
     * 抵押申请  开启流程
     *
     * @param bizPledgeInfo
     * @return
     */
    @Override
    public AjaxResult applyPledge(BizPledgeInfo bizPledgeInfo) {
        try {
            bizPledgeInfo.setPledgeId(UUID.randomUUID().toString());
            SysUser user = SecurityUtils.getLoginUser().getUser();
            //开启流程 流程INFo
            BizFlowInfo bizFlowInfo = buildFlowInfo(pledgeCreate, 1010, bizPledgeInfo.getReqId(), user);
            bizFlowInfoMapper.insertBizFlowInfo(bizFlowInfo);
            //流程历史
            BizFlowTask bizFlowTask = buildFlowTask(pledgeCreate, 1010, bizPledgeInfo.getReqId(), user, bizFlowInfo.getFlowUid());
            bizFlowTaskMapper.insertBizFlowTask(bizFlowTask);
            //新增抵押
            bizPledgeInfo.setFlowUid(bizFlowInfo.getFlowUid());
            bizPledgeInfo.setCreateBy(user.getUserId().toString());
            bizPledgeInfoMapper.insertBizPledgeInfo(bizPledgeInfo);
            return AjaxResult.success("申请成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("申请失败");
    }

    @Override
    public AjaxResult disablePledge(String flowUid, String phoneCode, String phone) {
        try {
            if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)){
                return AjaxResult.error("手机号或验证码必填！");
            }
                boolean result = checkPhoneCode(phone, phoneCode, pledgeDisable);
            if (!result) {
                return AjaxResult.error("验证码有误或已失效!");
            }
            BizFlowInfo selectFlowInfo = bizFlowInfoMapper.selectBizFlowInfoByFlowUid(flowUid);

            SysUser user = SecurityUtils.getLoginUser().getUser();
            BizFlowInfo bizFlowInfo1 = buildFlowInfo(pledgeDisable, 2000, selectFlowInfo.getBizId(), user);

            bizFlowTaskMapper.updateByFlowUid(null, flowUid);
            bizFlowInfoMapper.insertBizFlowInfo(bizFlowInfo1);
            BizFlowTask bizFlowTask = buildFlowTask(pledgeDisable, 2000, selectFlowInfo.getBizId(), user, bizFlowInfo1.getFlowUid());
            bizFlowTaskMapper.insertBizFlowTask(bizFlowTask);
            return AjaxResult.success("解押成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("解押失败！");
    }

    private BizFlowInfo buildFlowInfo(String bizType, Integer status, String bizId, SysUser user) {
        Long userId = user.getUserId();
        BizFlowInfo bizFlowInfo = new BizFlowInfo();
        bizFlowInfo.setBizType(bizType);
        bizFlowInfo.setBizId(bizId);
        bizFlowInfo.setFinanceId(user.getDept().getFinanceId());
        bizFlowInfo.setCurrentNodeUserId(String.valueOf(userId));
        bizFlowInfo.setCurrentNodeDeptId(String.valueOf(user.getDeptId()));
        bizFlowInfo.setStatus(status);
        bizFlowInfo.setCreateBy(String.valueOf(userId));
        return bizFlowInfo;
    }

    public BizFlowTask buildFlowTask(String bizType, Integer status, String bizId, SysUser user, String flowUid) {
        Long userId = user.getUserId();

        BizFlowTask bizFlowTask = new BizFlowTask();
        bizFlowTask.setStatus(status);
        bizFlowTask.setCreateTime(new Date());
        bizFlowTask.setCreateBy(String.valueOf(userId));
        bizFlowTask.setBizId(bizId);
        bizFlowTask.setBizType(bizType);
        bizFlowTask.setFlowUid(flowUid);
        bizFlowTask.setUserId(String.valueOf(userId));
        return bizFlowTask;
    }

    /**
     * 验证验证码
     *
     * @param phone     手机号
     * @param phoneCode 验证码
     * @return
     */
    public boolean checkPhoneCode(String phone, String phoneCode, String key) {
        String redisCode = redisCache.getCacheObject(phone + key);
        return phoneCode.equals(redisCode);
    }
}
